package com.andatsoft.laisim.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.andatsoft.laisim.R;
import com.andatsoft.laisim.theme.ThemeManager;
import com.andatsoft.laisim.utils.Animator;
import com.nineoldandroids.animation.Animator.AnimatorListener;

public class Toolbox extends FrameLayout {
	private Context mContext;
	private boolean mIsShowing;

	public Toolbox(Context context) {
		super(context);
		this.mContext = context;
		initViews();
	}

	public Toolbox(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		this.mContext = context;
		initViews();
	}

	public Toolbox(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.mContext = context;
		initViews();
	}

	private void initViews() {
		LayoutInflater inflater = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.dialog_message_confirm_background, this, true);
		if (getBackgroundLayout() != null) {
			if (ThemeManager.getSelectedTheme() != null) {
				getBackgroundLayout().setBackgroundColor(
						ThemeManager.getSelectedTheme()
								.getDialogBackgroundColor());
			}

			getBackgroundLayout().setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					close(null);
				}
			});
		}
	}

	public View getContent() {
		return getChildAt(1);
	}

	public LinearLayout getBackgroundLayout() {
		return (LinearLayout) getChildAt(0);
	}

	public void setContentLayout(int layoutId) {
		if (this.getChildCount() > 1)
			this.removeViewAt(1);
		LayoutInflater inflater = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(layoutId, this, true);
	}

	public boolean isShowing() {
		return mIsShowing;
	}

	public void show() {
		this.setVisibility(View.VISIBLE);
		if (getBackgroundLayout() != null)
			Animator.applyAlphaAnimation(getBackgroundLayout(), 350, null,
					0.0f, ThemeManager.getSelectedTheme().getTransitionAlpha());
		if (getContent() != null)
			Animator.applyAlphaAnimation(getContent(), 200, null, 0.0f, 1.0f);
	}

	// public void showWithAnim(float startX, final float startY, final float
	// dis) {
	// Rect r = new Rect();
	// getContent().getGlobalVisibleRect(r);
	// getContent().setPadding(0, (int) (startY - dis), 0, 0);
	// this.setVisibility(View.VISIBLE);
	//
	// Animator.applyAlphaAnimation(getBackgroundLayout(), 350, null, 0.0f,
	// ThemeManager.getSelectedTheme().getTransitionAlpha());
	// TranslateAnimation anim = new TranslateAnimation(startX, 0, startY,
	// startY - dis);
	//
	// AlphaAnimation anima = new AlphaAnimation(0.0f, 1.0f);
	//
	// AnimationSet an = new AnimationSet(false);
	// // an.setFillAfter(true);
	// an.setDuration(500);
	// an.addAnimation(anima);
	// an.addAnimation(anim);
	//
	// getContent().startAnimation(an);
	//
	// }

	public void close() {
		closeToolbox(null);
	}

	public void close(AnimatorListener event) {
		closeToolbox(event);
	}

	private void closeToolbox(AnimatorListener event) {
		Animator.applyAlphaAnimation(getBackgroundLayout(), 500, null,
				ThemeManager.getSelectedTheme().getTransitionAlpha(), 0.0f);
		if (event == null)
			event = mAnimatorEventEx;
		Animator.applyAlphaAnimation(getContent(), 200, event, 1.0f, 0.0f);

	}

	private void unbindDrawables(View view) {
		if (view.getBackground() != null) {
			view.getBackground().setCallback(null);
		}
		if (view instanceof ViewGroup) {
			for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
				unbindDrawables(((ViewGroup) view).getChildAt(i));
			}
			try {
				((ViewGroup) view).removeAllViews();
			} catch (UnsupportedOperationException ue) {

			}
		}
	}

	private com.nineoldandroids.animation.Animator.AnimatorListener mAnimatorEventEx = new com.nineoldandroids.animation.Animator.AnimatorListener() {

		@Override
		public void onAnimationStart(com.nineoldandroids.animation.Animator arg0) {

		}

		@Override
		public void onAnimationRepeat(
				com.nineoldandroids.animation.Animator arg0) {

		}

		@Override
		public void onAnimationEnd(com.nineoldandroids.animation.Animator arg0) {
			unbindDrawables(getContent());
			Toolbox.this.setVisibility(View.GONE);
			Toolbox.this.setClickable(false);
		}

		@Override
		public void onAnimationCancel(
				com.nineoldandroids.animation.Animator arg0) {

		}
	};

	@Override
	public void setVisibility(int visibility) {
		super.setVisibility(visibility);
		switch (visibility) {
		case View.VISIBLE:
			mIsShowing = true;
			break;
		case View.INVISIBLE:
			mIsShowing = false;
			break;
		case View.GONE:
			mIsShowing = false;
		default:
			mIsShowing = false;
			break;
		}
	};
}
